home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / Thread Manager Extension 2.0.1 / Sample Applications / Power Examples / NativeThreadTestApp / Source / NativeThreadTestApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-03  |  4.3 KB  |  245 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        NativeThreadTestApp.c
  3.  
  4.     Contains:    Test code for the NativeThreadTestApp.
  5.  
  6.     Written by:    Brad Post
  7.  
  8.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     9/14/93    bsp        Added comments and stuff
  13.          <1>     8/13/93    bsp        first checked in
  14. */
  15.  
  16. #include "NativeThreadTestApp.h"
  17.  
  18. /*
  19.  *  Globals needed for the test application.
  20.  */
  21.  
  22. QDGlobals            qd;
  23. MenuHandle            myMenus[3];
  24. WindowPtr            myWindow;
  25. Rect                myWindRect;
  26. short                x, y;    
  27. short                threadCounter;
  28. ThreadID            theThreadIDs[20];
  29.  
  30.  
  31. #define LoWord(x) ((short)(x))
  32. #define HiWord(x) ((short)((long)(x) >> 16))
  33.  
  34.  
  35. /*
  36.  *  aThread
  37.  *
  38.  *  This thread just finds out what it's ThreadID is and then prints it to the screen.
  39.  */
  40. pascal void *aThread( void *threadParameter )
  41. {
  42.     Str255    myThreadID;
  43.     ThreadID    theTID, aThreadID;
  44.     
  45.     aThreadID = *((ThreadID *) threadParameter);
  46.     
  47.     if( GetCurrentThread( &theTID ) != noErr )
  48.         myAlert("\pGetCurrentThread failed");
  49.     
  50.     NumToString( theTID, &myThreadID );
  51.     
  52.     if( theTID != aThreadID )
  53.         myAlert("\pThread ID's didn't match");
  54.         
  55.     while(1)
  56.     {
  57.         myNotify( myThreadID );
  58.         YieldToAnyThread();
  59.     }    
  60. }
  61.  
  62.  
  63. /*
  64.  *  doMenus
  65.  *
  66.  *  This function just handles menus.
  67.  */
  68. doMenus(long selector)
  69. {
  70.     short     whichMenu = HiWord(selector);
  71.     short     whichItem = LoWord(selector);
  72.     GrafPtr    savePort;
  73.     Str255    name;
  74.  
  75.     switch(whichMenu)
  76.     {
  77.         case appleMenuID:
  78.             switch(whichItem)
  79.             {
  80.                 case 1:    Alert(aboutALRT, 0L);
  81.                         break;
  82.                 default:
  83.                 {
  84.                     GetPort(&savePort);
  85.                     GetItem(myMenus[0], whichItem, name);
  86.                     OpenDeskAcc(name);
  87.                     SetPort(savePort);
  88.                 }
  89.             }
  90.             break;                
  91.         case fileMenuID:
  92.             switch(whichItem)
  93.             {
  94.                 case fmNew:
  95.                     createNewThread();
  96.                     break;
  97.                 case fmQuit:
  98.                     ExitToShell();
  99.                     break;
  100.             }
  101.             break;
  102.     }
  103. }    
  104.  
  105. //   Just the main routine
  106.  
  107. main()
  108. {
  109.     char        c;
  110.     short        done = 0;
  111.     short        whichWindow;
  112.     WindowPtr    theWindow;
  113.     EventRecord    myEvent;
  114.     Str255    myThreadID;
  115.     ThreadID    theTID;
  116.  
  117.     InitAll();
  118.     
  119.     if( GetCurrentThread( &theTID ) != noErr )
  120.         myAlert("\pGetCurrentThread failed");
  121.     
  122.     NumToString( theTID, &myThreadID );
  123.     
  124.     while(!done)
  125.     {
  126.         if(GetNextEvent(-1, &myEvent) == true)
  127.         {
  128.             switch(myEvent.what)
  129.             {
  130.                 case mouseDown: 
  131.                 {
  132.                     whichWindow = FindWindow(myEvent.where, &theWindow);
  133.                     switch(whichWindow)
  134.                     {
  135.                         case inSysWindow:
  136.                             SystemClick(&myEvent, theWindow);
  137.                             break;
  138.                         case inMenuBar:
  139.                             doMenus(MenuSelect(myEvent.where));
  140.                             break;
  141.                     }
  142.                 } 
  143.                     break;
  144.                 case keyDown:
  145.                 case autoKey:
  146.                     if((myEvent.modifiers & cmdKey) != 0)
  147.                         doMenus(MenuKey( (char) (myEvent.message & charCodeMask)));
  148.                     break;
  149.             }
  150.         }
  151.         myNotify( myThreadID );
  152.         YieldToAnyThread();
  153.     }
  154. }
  155.  
  156. /*
  157.  *  myAlert
  158.  *
  159.  *  Just throws up a ALRT box with a specified string.
  160.  */
  161. myAlert(Str255 theString)
  162. {
  163.     ParamText(theString, NULL, NULL, NULL);
  164.     Alert(errorALRT, NULL);
  165. }
  166.  
  167.  
  168. InitAll()
  169. {    
  170.     InitGraf( &(qd.thePort) );
  171.     InitFonts();
  172.     FlushEvents( everyEvent, 0);
  173.     InitWindows();
  174.     InitMenus();
  175.     TEInit();
  176.     InitDialogs(0L);
  177.     InitCursor();
  178.     MaxApplZone();
  179.  
  180.     myMenus[1] = GetMenu(fileMenuID);
  181.     InsertMenu(myMenus[0] = NewMenu(1, "\p\024"), 0);
  182.     InsertMenu(myMenus[1], 0);
  183.     AppendMenu(myMenus[0], "\pAbout NativeThreadTestApp;(-");
  184.     AddResMenu(myMenus[0], 'DRVR');
  185.     DrawMenuBar();
  186.  
  187.     myWindRect.top = 40;
  188.     myWindRect.bottom =  qd.screenBits.bounds.bottom - 20;        // windRect.top + 430
  189.     myWindRect.left = 10;
  190.     myWindRect.right = qd.screenBits.bounds.right - 20;         // windRect.left + 600
  191.     
  192.     myWindow = NewWindow(0L, &myWindRect, "\pTestAppWindow", true, documentProc, 
  193.                         (WindowPtr) -1, false, 0);
  194.     
  195.     x = myWindRect.left + 10;
  196.     y = myWindRect.top + 5;
  197.  
  198.     threadCounter = 0;
  199.     
  200.     createNewThread();
  201. }
  202.  
  203. /*
  204.  *  createNewThread
  205.  *
  206.  *  This function just creates a new cooperative thread.
  207.  */
  208. createNewThread()
  209. {
  210. #ifdef DEBUG
  211.     DebugStr("\pcreating a NewThread from App");
  212. #endif
  213.  
  214.     if( NewThread( kCooperativeThread, aThread, (void *) theThreadIDs[threadCounter], 0L, kCreateIfNeeded, 0L, theThreadIDs[threadCounter]) != noErr )
  215.         myAlert("\pGot an error creating a thread");
  216. }
  217.  
  218. /*
  219.  *  myNotify
  220.  *  
  221.  *  Just prints out the message to the the window.
  222.  */
  223. myNotify( Str255 theMessage )
  224. {
  225.     SetPort( myWindow );
  226.     
  227.     PenNormal();
  228.     TextFont(geneva);
  229.     TextSize(12);
  230.     
  231.     if( (y + 20) > myWindRect.bottom )
  232.     {
  233.         EraseRect(&(**myWindow->visRgn).rgnBBox);
  234.         x = myWindRect.left + 10;
  235.         y = myWindRect.top + 10;
  236.     }
  237.     
  238.     MoveTo(x, y);
  239.     
  240.     DrawString( theMessage );
  241.     
  242.     y += 10;
  243.     
  244. }
  245.